package org.rsbot.event.impl;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;

import org.rsbot.client.Node;
import org.rsbot.client.RSNPCNode;
import org.rsbot.bot.Bot;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Calculations;
import org.rsbot.script.Methods;
import org.rsbot.script.wrappers.RSNPC;

public class DrawNPCs implements PaintListener {

    Methods ctx;

    public DrawNPCs(Bot bot) {
        ctx = bot.getMethods();
    }

    public void onRepaint(Graphics render) {
        if (!ctx.isLoggedIn()) {
            return;
        }
        FontMetrics localFontMetrics = render.getFontMetrics();
        for (int k : Bot.getClient().getRSNPCIndexArray()) {
            Node localNode = Calculations.findNodeByID(Bot.getClient().getRSNPCNC(), k);
            if (localNode == null || !(localNode instanceof RSNPCNode)) {
                continue;
            }
            RSNPC localRSNPC = new RSNPC(((RSNPCNode) localNode).getRSNPC());
            Point localPoint = localRSNPC.getScreenLocation();
            if (!ctx.pointOnScreen(localPoint)) {
                continue;
            }
            render.setColor(Color.RED);
            render.fillRect((int) localPoint.getX() - 1, (int) localPoint.getY() - 1, 2, 2);
            String str = "ID: [ " + localRSNPC.getID() + " ]";
            render.setColor((localRSNPC.isInCombat()) ? Color.RED : (localRSNPC.isMoving()) ? Color.ORANGE : Color.GREEN);
            render.drawString(str, localPoint.x - localFontMetrics.stringWidth(str) / 2, localPoint.y - localFontMetrics.getHeight() / 2);

            if (localRSNPC.getAnimation() != -1 || localRSNPC.getGraphic() != -1) {
                str = "[Ani] : " + localRSNPC.getAnimation() + " | [Graph] : " + localRSNPC.getGraphic();
                render.drawString(str, localPoint.x - localFontMetrics.stringWidth(str) / 2, localPoint.y - localFontMetrics.getHeight() * 3 / 2);
            }
        }
    }
}
